package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.AppointWorkersToStagesForm;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;
/**
 * Controlls the Appoint worker to Stage use case 
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
public class AppointWorkersToStagesController implements FormListener{
	private AppointWorkersToStagesForm appointWorkersForm;
	private AppointWorkersToStagesForm appointTesterForm;
	private RequsetInfoFrame  reqFram;	
	private  HashMap<String,String> workers  ;
	private ArrayList<String> workerlist;
	
	@Override
	public void onFormCanceled(FormFrame formFrame) {
		if (formFrame==appointWorkersForm) {
			appointWorkersForm.dispose();
			appointWorkersForm=null;
		}
		if (formFrame==appointTesterForm) {
			appointTesterForm.dispose();
			appointTesterForm=null;
		}			
	}
	
	@Override
	public void onFormSubmited(final FormFrame formFrame, ArrayList<Object> values) {
		if (appointWorkersForm ==formFrame){
			if (values.get(0).equals(values.get(1))){
				appointWorkersForm.showMessege("Appoint two different workers please!");
				values.clear();
			}
			else{
			try {
				ArrayList<Object> uids=new ArrayList<Object>();
				java.util.Iterator<Object> itr = values.iterator();
				uids.add(appointWorkersForm.getReqid());
				while(itr.hasNext()) 
					uids.add(workers.get(itr.next()));
				values=uids;
				Client c = Client.getInstance();
				Mssg mssg = new Mssg(P.AppointReqWorkers,values);
				c.sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							appointWorkersForm.showMessege((String) mssg.getMessege());
							return;
						}
						JOptionPane.showMessageDialog(appointWorkersForm,Resources.Strings.AppointedSuccesfully);
						formFrame.dispose();
						formFrame.dispose();
						reqFram.dispose();
					}
				},mssg);
			 
			} catch (Exception e) {
				appointWorkersForm.showMessege(Resources.Strings.ConnectFailed);
			}
			}
		}
		else if(formFrame==appointTesterForm){
			try {
				values.add(workers.get(values.get(0)));
				values.add(appointTesterForm.getReqid());
				Client c = Client.getInstance();
				Mssg mssg = new Mssg(P.AppointTester,values);
				c.sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							appointTesterForm.showMessege((String) mssg.getMessege());
							return;
						}
						JOptionPane.showMessageDialog(appointTesterForm,Resources.Strings.AppointedSuccesfully);
						formFrame.dispose();
						formFrame.dispose();
						reqFram.dispose();
					}
				},mssg);
			 
			} catch (Exception e) {
				appointTesterForm.showMessege(Resources.Strings.ConnectFailed);
			}
		}

	}
/**
 * executes view appoint workers
 * @param req
 * @param requsetInfoFrame
 */
	public void execViewAppointWorkers(final ChangeRequest req, RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		try {
			Mssg msg = new Mssg(P.usersNameByTid,"'worker'");
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@SuppressWarnings("unchecked")
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null, mssg.getMessege());
						return;
						}
					if ((workers = new  HashMap<String,String>((HashMap<String,String>)mssg.getMessege())) == null) 	
						JOptionPane.showMessageDialog(null,String.format(Resources.Strings.NoUsersFound));
					else
						{
						workerlist=new ArrayList<String>(workers.keySet());
						mssg = new Mssg(P.systemWorker,"'"+req.getValue("Information System")+"'");
						try {
							Client.getInstance().sendToServer(new ServerResponseListener() {
								@SuppressWarnings({ "unused" })
								@Override
								public void onServerResponse(Mssg mssg) {
									String surveyor;
									if (mssg.is(P.error)) {
										JOptionPane.showMessageDialog(null, mssg.getMessege());
										return;
										}
									if ((surveyor = new String((String) mssg.getMessege())) != null){
										appointWorkersForm=new AppointWorkersToStagesForm(workerlist,req,surveyor);
										appointWorkersForm.addFormListener(AppointWorkersToStagesController.this);
										appointWorkersForm.showFrame(200,370);
									}
									else
										JOptionPane.showMessageDialog(null,String.format(Resources.Strings.NoUsersFound));
								}
							}, mssg);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						}
				}
			}, msg);
			

			} 
		catch (Exception e) {
			e.printStackTrace();
			}
			
	}
	/**
	 * executes view appoint tester
	 * @param reqid
	 * @param requsetInfoFrame
	 */
	public void execViewAppointTester(final String reqid, RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		try {
			Mssg msg = new Mssg(P.usersNameByTid,"'b.member'");
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@SuppressWarnings("unchecked")
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null, mssg.getMessege());
						return;
						}
					if ((workers = new  HashMap<String,String>((HashMap<String,String>)mssg.getMessege())) != null) {						
						appointTesterForm=new AppointWorkersToStagesForm(new ArrayList<String>(workers.keySet()).toArray(new String[workers.size()]),reqid,"tester");
						appointTesterForm.addFormListener(AppointWorkersToStagesController.this);
						appointTesterForm.showFrame(150,370);
					} else
						JOptionPane.showMessageDialog(null,String.format(Resources.Strings.NoUsersFound));
				}
			}, msg);
			} 
		catch (Exception e) {
			e.printStackTrace();
			}
			
	}



}
